/*
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: CurrentEntrustOrderFragment.java
 *   @Date: 18-11-29 下午4:05
 *   @Author: ppzhao
 *   @Description:
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 */

package io.bhex.app.account.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.bhex.app.R;
import io.bhex.baselib.utils.PixelUtils;
import io.bhex.sdk.trade.bean.OrderBean;
import io.bhex.app.account.presenter.CurrentEntrustFragmentPresenter;
import io.bhex.app.base.BaseFragment;
import io.bhex.app.event.RevokeOrderEvent;
import io.bhex.app.trade.adapter.OpenOrdersAdapter;

/**
 * ================================================
 * 描   述：委托订单
 * ================================================
 */

public class CurrentEntrustOrderFragment extends BaseFragment<CurrentEntrustFragmentPresenter, CurrentEntrustFragmentPresenter.EntrustOrderUI> implements CurrentEntrustFragmentPresenter.EntrustOrderUI, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {
    private RecyclerView recyclerView;
    private OpenOrdersAdapter adapter;
    private SmartRefreshLayout swipeRefresh;
    private View emptyView;
    //当前显示的订单
    private List<OrderBean> showOrders;

    @Override
    protected CurrentEntrustFragmentPresenter.EntrustOrderUI getUI() {
        return this;
    }

    @Override
    protected CurrentEntrustFragmentPresenter createPresenter() {
        return new CurrentEntrustFragmentPresenter();
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_entrust_order_layout, null, false);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RevokeOrderEvent event){
        getPresenter().revokeAllOrders();
    }

    @Override
    protected void initViews() {
        super.initViews();
        swipeRefresh = viewFinder.find(R.id.swipeRefresh);
        swipeRefresh.setOnRefreshListener(this);
        recyclerView = viewFinder.find(R.id.recyclerView);

        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        emptyView = layoutInflater.inflate(R.layout.empty_layout, swipeRefresh, false);
        ViewGroup.LayoutParams layoutParams = emptyView.getLayoutParams();
        layoutParams.height = PixelUtils.dp2px(200);
        emptyView.setLayoutParams(layoutParams);

    }

    @Override
    protected void addEvent() {
        super.addEvent();

    }

    @Override
    public void onLoadMoreRequested() {
//        swipeRefresh.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                adapter.loadComplete();
//            }
//        }, 500);
        getPresenter().loadMore();
    }

    @Override
    public void loadMoreComplete() {
        if (adapter != null) {
            adapter.loadMoreComplete();
        }
    }

    @Override
    public void loadMoreFailed() {
        if (adapter != null) {
            adapter.loadMoreFail();
        }
    }

    @Override
    public void loadEnd() {
        if (adapter != null) {
            adapter.loadMoreEnd();
        }
    }

    @Override
    public List<OrderBean> getShowOrders() {
        return showOrders;
    }


    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        getPresenter().getCurrentEntrustOrders(false);
        refreshLayout.finishRefresh(1000);
    }

    //    public void setRefreshing(final boolean refreshing) {
//        swipeRefresh.post(new Runnable() {
//            @Override
//            public void run() {
//                swipeRefresh.setRefreshing(refreshing);
//            }
//        });
//    }

    @Override
    public void showOrders(List<OrderBean> currentOrders) {
        showOrders = currentOrders;
        if (adapter == null) {
//            if (currentOrders != null) {
//                if (currentOrders.isEmpty()) {
//                    currentOrders = null;
//                }
//            }
            adapter = new OpenOrdersAdapter(currentOrders);
//            adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
            adapter.isFirstOnly(false);
            adapter.setOnLoadMoreListener(this,recyclerView);
            adapter.setEnableLoadMore(true);
//            swipeRefresh.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
//            swipeRefresh.setOnRefreshListener(this);

            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
//        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));

            adapter.setEmptyView(emptyView);
            recyclerView.setAdapter(adapter);
            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    if (view.getId() == R.id.revoke_order) {
                        OrderBean itemModel = (OrderBean) adapter.getData().get(position);
                        getPresenter().cancelOrder(itemModel.getOrderId(),false);
                    }
                }
            });
        } else {
            adapter.setNewData(currentOrders);
        }
    }

}
